﻿using RentACarRESTApi.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;

namespace RentACarRESTApi.Services
{
    public class CarsRepository
    {
        private List<Car> cars;

        public CarsRepository()
        {
            cars = new List<Car>()
            {
                new Car(id: 1, name:"a", make: "Chevrolet", model: "Aveo"),
                new Car(id: 2, name:"b", make: "Volkswagen", model: "Golf"),
                new Car(id: 3, name:"c", make: "Mercedes Benz", model: "E300 Coupe")
            };            
        }

        public IEnumerable<Car> GetCars()
        {
            return cars;
        }

        public Car GetCarById(int id)
        {
            try
            {
                return cars.Single(x => x.ID == id);
            }
            catch(InvalidOperationException ex)
            {
               var resp = new HttpResponseMessage(HttpStatusCode.NotFound)
                {
                    Content = new StringContent(string.Format("No car with ID = {0}", id)),
                    ReasonPhrase = "Car ID Not Found"
                };

               throw new System.Web.Http.HttpResponseException(resp);
            }   
        }
    }
}